package com.hepinghospital.user.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.hepinghospital.user.ui.activity.userinfo.ChangePhoneActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;

import jz.joyoung.robot.util.AES128;
import jz.joyoung.robot.util.UtilString;


/**
 * @time 2020/11/19 14:27
 */

public class CodeChangeDialog extends BaseDialogFragment {

    private Handler mHandler = new Handler();
    int second = 60;

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_confirm, btn_cancel, tv_message, btn_code, btn_change_phone;
    private EditText edt_code;
    private String patientId, mPhone,id;

    public static CodeChangeDialog newInstance(String patientId,String id, String phone) {
        Bundle args = new Bundle();
        args.putString("patientId", patientId);
        args.putString("id", id);
        args.putString("phone", phone);
        CodeChangeDialog fragment = new CodeChangeDialog();
        fragment.setArguments(args);
        return fragment;
    }


    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_change_code, container);
        btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        tv_message = view.findViewById(R.id.tv_message);
        btn_code = view.findViewById(R.id.btn_code);
        btn_change_phone = view.findViewById(R.id.btn_change_phone);
        edt_code = view.findViewById(R.id.edt_code);
        if (getArguments() != null) {
            patientId = getArguments().getString("patientId");
            id = getArguments().getString("id");
            mPhone = getArguments().getString("phone");
        }
        btn_cancel.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }
        });

        btn_confirm.setOnClickListener(v -> {
            if (mListener != null) {
                if (!UtilString.isEmpty(edt_code.getText().toString())) {
                    mListener.onConfirm(edt_code.getText().toString());
                    dismiss();
                } else {
                    ToastUtil.showToast(mContext, "请先输入验证码");
                }

            }
        });
        btn_code.setOnClickListener(v -> {
            second = 60;
            getCode();
        });

        btn_change_phone.setOnClickListener( v -> {
            ApplyPermissionDialog applyPermissionDialog = ApplyPermissionDialog.newInstance( "2", true );
            applyPermissionDialog.show( getChildFragmentManager(), "applyPermissionDialog" );
            applyPermissionDialog.setmListener( new ApplyPermissionDialog.OnItemClickListener() {

                @Override
                public void onConfirm() {
                    Intent intent = new Intent( getActivity(), ChangePhoneActivity.class );
                    intent.putExtra( "patientId", patientId );
                    intent.putExtra( "id", id );//家庭成员id
                    Log.e( "手机号-----", mPhone );
                    intent.putExtra( "endPhoneNo", mPhone );
                    startActivity( intent );
                }

                @Override
                public void onCancel() {

                }
            } );

        } );
        setInfo();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            second--;
            if (second != 0) {
                mHandler.postDelayed(runnable, 1000);
                btn_code.setEnabled(false);
                btn_code.setText(second + "秒后重试");
            } else {
                btn_code.setText("发送验证码");
                btn_code.setEnabled(true);
            }
        }
    };

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @SuppressLint("SetTextI18n")
    private void setInfo() {
        tv_message.setText("您在医院预留手机号为" + UtilString.hidePhone(mPhone) + ",与您当前手机号不一致,系统将向您预留手机号发送验证码确认身份");
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String code);

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    private void getCode() {
        try {
            RestProxy.getInstance().getCode(AES128.encryptStringAES(mPhone, "7t3e506jaa10xbd4"), new BaseObserver<String>() {
                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(getActivity(), e);

                }

                @Override
                public void onSuccess(String result) {
                    btn_code.setEnabled(false);
                    mHandler.postDelayed(runnable, 1000);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
